package ext.tianma.part.processors;

import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import wt.method.RemoteAccess;
import wt.part.WTPart;
import wt.util.WTException;
import wt.util.WTProperties;

import com.ptc.core.components.beans.ObjectBean;
import com.ptc.core.components.forms.DefaultObjectFormProcessor;
import com.ptc.core.components.forms.FormProcessingStatus;
import com.ptc.core.components.forms.FormResult;
import com.ptc.core.components.util.FeedbackMessage;
import com.ptc.core.ui.resources.FeedbackType;
import com.ptc.netmarkets.model.NmOid;
import com.ptc.netmarkets.util.beans.NmCommandBean;

import ext.com.iba.IBAUtil;
import ext.tianma.part.forms.TMTypeUtil;
import ext.tianma.part.forms.TransitFactoryUtil;

public class RAWTransitFactoryProcessor  extends DefaultObjectFormProcessor implements RemoteAccess, Serializable
{
	private static boolean VERBOSE = false;
    private static String CLASSNAME = RAWTransitFactoryProcessor.class.getName();
    private static final String RESOURCE = "ext.tianma.resource.ResourceRB";
    //物料英文名称
    private static final String PART_E_NAME = "partEName";
    
    static
    {
        try
        {
            WTProperties wtproperties = WTProperties.getLocalProperties();
            VERBOSE = wtproperties.getProperty("ext.tianma.part.verbose", false);
        }
        catch (Throwable throwable)
        {
            throwable.printStackTrace(System.err);
            throw new ExceptionInInitializerError(throwable);
        }
    }
	@Override
	public FormResult doOperation(NmCommandBean arg0, List<ObjectBean> arg1) throws WTException {
		FormResult formresult = new FormResult();
		formresult.setStatus(FormProcessingStatus.SUCCESS);
	
		NmOid nmoid = arg0.getActionOid();
		WTPart currentPart = (WTPart) nmoid.getRefObject();
//		//无效的代码
//		// String number = currentPart.getNumber();
//		System.out.println("Debug  BOM转厂   当前进阶的物料编号："+currentPart.getNumber());
		HttpServletRequest request = arg0.getRequest();
//		/*ArrayList factoryList =(ArrayList)arg0.getChecked().get("factory");
//		for(int i=0;i<factoryList.size();i++)
//		{
//			Object obj = factoryList.get(i);
//			System.out.println("Debug  BOM转厂   选择的生效地："+obj.toString());
//		}*/
		String[] factorys = request.getParameterValues("factory");
//		if(null == factorys||factorys.length==0)
//		{
//			throw new WTException("没有选择物料生效的请选择！");
//		}
//		String factory = "";
//		for(int i = 0;i<factorys.length;i++)
//		{
//			String temp = factorys[i];
//			if(factory.equals(""))
//			{
//				factory = temp;
//			}
//			else
//			{
//				factory = factory+","+temp;
//			}
//			//System.out.println("Debug  BOM转厂   选择的生效地："+temp);
//		}
//		System.out.println("Debug  原材料转厂   选择的生效地："+factory);
//		try {
//			String internalName=ClientTypedUtility.getTypeIdentifier(currentPart).getTypename();
//			if(internalName.contains(".RawMaterial")){
//				
//				currentPart = CoreUtil.getWTPartByNumberAndView(currentPart.getNumber(), currentPart.getViewName());
//				
//				//升级版本
//				currentPart =(WTPart)CoreUtil.checkoutObject(currentPart);
//				
//				PartCommands.setEffectiveSite( currentPart , PartCommands.getConfigSpec(), factory);
//			
//				currentPart =(WTPart)CoreUtil.checkinObject(currentPart,"升级版本添加属性");
//			}
//			
//		} catch (WTPropertyVetoException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		if( TMTypeUtil.isRawMaterial( currentPart ) ){
			
			if( !hasAttributesFill( currentPart ) ){
				throw new WTException( "物料英文名称必填！" );
			}
			
		}
		try {
			TransitFactoryUtil.tansitPartAndChildrenFactory( currentPart , factorys );
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		FeedbackMessage feedbackmessage = new FeedbackMessage(
				FeedbackType.SUCCESS, null, "原材料转厂成功", null, new String[] {});
		formresult.addFeedbackMessage(feedbackmessage);
		return formresult;
	}
	
	/**
	 * 检查原材料英文名称如果为空则不能进行转厂
	 * @param part
	 * @return
	 */
	private static boolean hasAttributesFill( WTPart part ){
		
		boolean hasAttributesFill = false ;
		
		if( part == null ){
			return hasAttributesFill;
		}
		
		try {
			String partEName = (String)IBAUtil.getIBAValue( part , PART_E_NAME );
			
			System.out.println( "Debug   partEName = "+partEName );
			
			if( partEName != null && !partEName.trim().isEmpty() ){
				hasAttributesFill = true;
			}
			
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (WTException e) {
			e.printStackTrace();
		}
		
		return hasAttributesFill;
	}
}

